#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
#define ZVALUE 17576
int edges[ZVALUE][ZVALUE];
int mapp[ZVALUE];
int degree[ZVALUE];
int visited[ZVALUE];
int totalWeight;
int maxDegreeIndex;
int maxDegree;
int numcount;
int string2int(string x){
    return (x[0]-'A')*26*26+(x[1]-'A')*26+(x[2]-'A');
}
string int2string(int x){
    string s="";
    s+=(x/26/26)+'A';
    s+=(x/26%26)+'A';
    s+=(x%26)+'A';
    return s;
}
void dfs(int i){
    numcount++;
    visited[i]=1;
    totalWeight+=degree[i];
    if(maxDegree<degree[i]){
        maxDegree=degree[i];
        maxDegreeIndex=i;
    }
    for(int j=0;j<ZVALUE;j++){
        if(mapp[j]==1&&visited[j]==0&&edges[i][j]>0){
            dfs(j);
        }
    }
}
bool msort(const pair<int,int> &a,const pair<int,int> &b){
    return a.first<b.first;
}
int main(){
    int n,k;
    cin>>n>>k;
    int lastIndex=0;
    while(n-->0){
        string a,b;
        int c;
        cin>>a>>b>>c;
        int ta = string2int(a);
        int tb = string2int(b);
        edges[ta][tb]+=c;
        edges[tb][ta]+=c;
        degree[ta]+=c;
        degree[tb]+=c;
        mapp[ta]=1;
        mapp[tb]=1;
    }
    //cout<<"=====================================\n";
    vector<pair<int,int> > res;
    for(int i=0;i<ZVALUE;i++){
        if(mapp[i]==1){
            if(visited[i]==0){
                numcount=0;
                totalWeight=0;
                maxDegreeIndex=0;
                maxDegree=0;
                dfs(i);
                //cout<<int2string(maxDegreeIndex)<<" "<<numcount<<" "<<totalWeight<<endl;
                if(numcount<=2||totalWeight<=2*k)
                    continue;
                res.push_back(pair<int,int>(maxDegreeIndex,numcount));
            }
        }
    }
    //cout<<"=====================================\n";
    int ressize=res.size();
    cout<<ressize<<endl;
    if(ressize>0){
        sort(res.begin(),res.end(),msort);
        for(int i=0;i<ressize;i++){
            cout<<int2string(res[i].first)<<" "<<res[i].second<<endl;
        }
    }
    return 0;
}
